﻿window.$jscript.fn.Login = (function () {
    var __FN = function () {
        return new __FN.fn._init();
    };
    var hxt = window.$jscript.fn;
    __FN.fn = __FN.prototype = {
        _init: function () { return this; },
        param: { key: '',link:''},
        ready: function (options) {
            var self = this;
            hxt.ready({ link: options.link, reqType: 0, key: self.param.key });
            self.initPage();
        },
        initPage: function () {
            var self = this;
            hxt.element.elVue = new Vue({
                data: function () {
                    var _this = this;
                    return {
                        form: {
                            account: '',
                            pwd: '',
                            code: ''
                        },
                        code: '',
                        
                    };
                },
                watch: {

                },
                computed: {},
                mounted: function () {
                    var _this = this;
                    _this.loadCode();
                    if (!!localStorage) {
                        _this.form.account = localStorage.getItem("last-login-account");
                    }
                    //回车登录事件
                    $(document).keydown(function (event) {
                        if (event.keyCode == 13) {
                            _this.onlog();
                        }
                    });
                },
                methods: {
                    loadCode: function () {
                        var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
                        var arr = str.split("");
                        var oldStr = "";
                        for (var i = 0; i < 4; i++) {
                            var n = Math.floor(Math.random() * arr.length);
                            oldStr += arr[n];
                        }
                        this.code = oldStr.substring(oldStr.length - 4);
                        this.form.code = this.code;
                    },
                    onlog: function () {
                        var _this = this, form = _this.form;
                        if (!form.account || form.account.length < 2) {
                            _this.$refs['account'].focus();
                            return hxt.error(!form.account ? '请输入账号' : '账号不正确');
                        }
                        if (!form.pwd || form.pwd.length < 4) {
                            _this.$refs['pwd'].focus();
                            return hxt.error(!form.pwd ? '请输入密码' : '密码不正确');
                        }
                        if (!form.code) {
                            _this.$refs['code'].focus();
                            return hxt.error('请输入验证码');
                        }
                        if (form.code.toUpperCase() != _this.code.toUpperCase()) {
                            _this.$refs['code'].focus();
                            form.code = '';
                            _this.loadCode();
                            return hxt.error('验证码不正确');
                        };
                        hxt.post({
                            url: hxt.config.link + 'pc/webLogin',
                            data: { account: form.account, pwd: form.pwd },
                            ok: function (res) {
                                if (res.code != 0) {
                                    form.code = '';
                                    _this.loadCode();
                                    return hxt.alert(res.errmsg);
                                }
                                if (!!localStorage) localStorage.setItem("last-login-account", form.account);
                                hxt.mask('正在登录系统，请稍后...');
                                window.location.replace('index?key=' + hxt.toBase64(res.data.openkey,true));
                            }
                        });
                    }
                },
                components: {
                    schoolButton: function () { return hxt.staticComponent(self.param.components.schoolButton); }
                }
            }).$mount('#app');
        }
    };
    __FN.fn._init.prototype = __FN.fn;
    return __FN.fn;
})();